﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BooleanDescriptorAttribute.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Attributes
{
    using System.Reflection;

    using RadicalResearch.MiniState.Descriptors;

    /// <summary>Describes a Boolean setting stored as a bit within a bitmask.</summary>
    public class BooleanDescriptorAttribute : SettingDescriptorAttribute
    {
        #region Constants and Fields

        /// <summary>
        ///   Index of the bit within the bitmask
        /// </summary>
        private readonly int bitIndex;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="BooleanDescriptorAttribute"/> class.</summary>
        /// <param name="bitIndex">Index of the bit within bitmask</param>
        public BooleanDescriptorAttribute(int bitIndex)
        {
            this.bitIndex = bitIndex;
        }

        /// <summary>Initializes a new instance of the <see cref="BooleanDescriptorAttribute"/> class.</summary>
        /// <param name="settingKey">The setting key.</param>
        /// <param name="bitIndex">Index of the bit within bitmask.</param>
        public BooleanDescriptorAttribute(string settingKey, int bitIndex)
            : base(settingKey, false)
        {
            this.bitIndex = bitIndex;
        }

        /// <summary>Initializes a new instance of the <see cref="BooleanDescriptorAttribute"/> class.</summary>
        /// <param name="settingKey">The setting key.</param>
        /// <param name="valueKey">The value key.</param>
        /// <param name="bitIndex">Index of the bit within bitmask.</param>
        public BooleanDescriptorAttribute(string settingKey, string valueKey, int bitIndex)
            : base(settingKey, valueKey, false)
        {
            this.bitIndex = bitIndex;
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Gets the index of the bit.
        /// </summary>
        /// <value>
        ///   The index of the bit.
        /// </value>
        protected int BitIndex
        {
            get
            {
                return this.bitIndex;
            }
        }

        #endregion

        #region Methods

        /// <summary>Gets the descriptor.</summary>
        /// <param name="property">The property.</param>
        /// <returns>Setting descriptor for the specified property.</returns>
        internal override SettingDescriptorBase GetDescriptor(PropertyInfo property)
        {
            if (!typeof(bool).IsAssignableFrom(property.PropertyType))
            {
                throw new SettingStoreException("Property is not a Boolean type");
            }

            return new BooleanSettingDescriptor(this.ValueKey, this.BitIndex);
        }

        #endregion
    }
}